/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.kucukcezve.tabu;

import net.kucukcezve.tabu.stopwatch.StopWatchObserver;
import net.kucukcezve.tabu.stopwatch.StopWatchThread;

/**
 *
 * @author cemal
 * @mail : cemal.yilmaz@gmail.com
 */
public class Game implements StopWatchObserver {

    // canvas
    private TabuCanvas canvas;
    //stopwatch
    //
    private StopWatchThread stopWatch;
    // cards
    private Card[] cards;
    private int cardIndex = 0;
    private Card currentCard;
    // teams
    private Team[] teams;
    private Team currentTeam;
    private GameTurn currentTurn;
    //
    private int gameState = PAUSED;
    public final static int PAUSED = 1;
    public final static int PLAYING = 2;

    // cba's
    private CBA menuBarPlaying;
    private CBA menuBarPaused;

    public void timeUpdate(String newTime) {
        canvas.updateStopWatchString(newTime);
    }

    public Game(TabuCanvas _canvas) {
        canvas = _canvas;
        //
        cards = new Card[4];
        cards[0] = new Card(new String[]{"Kelime1",
                    "Yasak1", "Yasak2", "Yasak3", "Yasak4", "Yasak5",});
        cards[1] = new Card(new String[]{"Kelime2",
                    "Yasak1", "Yasak2", "Yasak3", "Yasak4", "Yasak5",});
        cards[2] = new Card(new String[]{"Kelime3",
                    "Yasak1", "Yasak2", "Yasak3", "Yasak4", "Yasak5",});
        cards[3] = new Card(new String[]{"Kelime4",
                    "Yasak1", "Yasak2", "Yasak3", "Yasak4", "Yasak5",});
        currentCard = cards[0];
        teams = new Team[]{new Team("Team A"), new Team("Team B")};
        currentTurn = new GameTurn();

//
        menuBarPlaying = new CBA("Devam", "Yanlış", "Pass");
        menuBarPaused = new CBA("Başla", "", "");


        stopWatch = new StopWatchThread(this);
        stopWatch.start();
    //stopWatch.startWatch();
    }

    public void correctCard() {
        currentTurn.correctCard();
        cardIndex++;
        cardIndex %= cards.length;
        currentCard = cards[cardIndex];
    }

    public void passCard() {
        currentTurn.passCard();
        cardIndex++;
        cardIndex %= cards.length;
        currentCard = cards[cardIndex];
    }

    public void pause() {
        gameState = PAUSED;
    }

    public void play() {
        gameState = PLAYING;
        stopWatch.startWatch();
    }

    public int gameState() {
        return gameState;
    }

    public void wrongCard() {
        currentTurn.wrongCard();
        cardIndex++;
        cardIndex %= cards.length;
        currentCard = cards[cardIndex];
    }

    public Card getCurrentCard() {
        return currentCard;
    }

    public Team getTeam(int index) {
        return teams[index];
    }

    public boolean canPass() {
        return currentTurn.canPass();
    }

    public CBA getCBA() {
        if (gameState == PAUSED) {
            return menuBarPaused;
        } else if (gameState == PLAYING) {
            return menuBarPlaying;
        }
        return menuBarPlaying;
    }

    void handleLeftSoftKey() {
        if (gameState == PAUSED) {
            play();
        } else if (gameState == PLAYING) {
            correctCard();
        }
    }

    void handleMiddleSoftKey() {
        if (gameState == PLAYING) {
            wrongCard();
        }
    }

    void handleRightSoftKey() {
        if (gameState == PAUSED) {
        } else if (gameState == PLAYING) {
            passCard();
        }
    }
}
